package com.z.sys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.z.sys.entity.Dept;
import com.z.sys.vo.DeptExcelVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 机构管理Mapper接口
 *
 * @author z
 * @date 2022-11-07
 */
@Mapper
public interface DeptMapper extends BaseMapper<Dept> {
    /**
     * 查询机构管理列表
     *
     * @param dept 机构管理
     * @return 机构管理集合
     */
    List<Dept> selectDeptList(Dept dept);

    /**
     * 根据人员id和角色名称查询管辖机构
     * @param roleName
     * @param userId
     * @return
     */
    @Select("SELECT d.id,d.name_abbr,d.full_path,'0' parent_id " +
            "FROM t_dept d " +
            "LEFT JOIN t_dept_user_role td on d.id = td.dept_id " +
            "LEFT JOIN t_role r on td.role_id = r.id " +
            "where td.del=0 and d.del=0 and d.status=1 and r.`name`=#{roleName} and td.user_id=#{userId}")
    List<Dept> selectDeptByRoleName(@Param("roleName") String roleName, @Param("userId")Long userId);
    /**
     * 根据人员id查询所在单位
     * @param userId
     * @return
     */
    @Select("SELECT d.id,d.name_abbr,'0' parent_id FROM t_dept d " +
            "where d.code =(SELECT left(d.code,if(instr(d.code,'-')>1,instr(d.code,'-')-1,LENGTH(d.code)))  " +
            "FROM t_dept d LEFT JOIN t_user u on d.id=u.dept_id " +
            "where d.del=0 and u.del=0 and d.status=1 and u.id=#{userId})")
    List<Dept> selectUnitByUserId( Long userId);

    /**
     * 查询机构管理列表
     *
     * @param dept 机构管理
     * @return 机构info集合
     */
    List<DeptExcelVo> selectDeptInfoList(Dept dept);

    /**
     * 调用存储过程更新 fullPath
     * @return
     */
    void updateFullPath();
}
